﻿using Ext.Net;
using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using NOAH.CMS.Repository;
using NOAH.CMS.Cache;
using NOAH.CMS.Infracstructure;

public partial class admin_Modules_Course_AddOrEdit : System.Web.UI.Page
{
    private string id = string.Empty;

    public void AddOrEdit(object sender, DirectEventArgs e)
    {
        try
        {
            id = base.Request.QueryString["id"] ?? "0";

            //X.Msg.Alert("ID", id).Show();
            //return;

            using (CourseRepository repository = new CourseRepository())
            {
                Course entity = null;
                if (this.id != "0")
                {
                    entity = repository.Get(int.Parse(this.id));
                }
                else
                {
                    entity = new Course();
                }
                entity.Name = txtName.Text;
                entity.Content = e.ExtraParams["fckParameter"].ToString();

                int temp = 0;
                if (txtSubject.Value != null) int.TryParse(txtSubject.Value.ToString(), out temp);
                entity.SubjectID = temp;

                entity.ShortContent = txtShortContent.Text;

                if (dtStart.SelectedValue != null)
                {
                    entity.DateStart = dtStart.SelectedDate;
                }
                entity.HasExpired = chkExpired.Checked;

                if (txtNumberOfLearner.Value != null)
                    entity.NumberOfLearner = Convert.ToInt16(txtNumberOfLearner.Value);

                if (txtPrice.Value != null)
                {
                    entity.Price = Convert.ToDouble(txtPrice.Value);
                }

                if (this.id == "0")
                {
                    entity.DateCreated = DateTime.Now;
                    repository.Add(entity);
                }
                repository.SubmitChanges();

                if (this.id != "0")
                {
                    BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
                    BasePage.RegisterCloseWindow(this.ScriptManager2);
                }
                else
                {
                    Resetcontrol();
                }

                ExtNet.Msg.Hide();
                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
                BasePage.RegisterCloseWindow(this.ScriptManager2);
            }
        }
        catch (Exception ex)
        {
            X.Msg.Alert("Eror", ex.Message + ex.StackTrace).Show();
            //Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    private void Resetcontrol()
    {
        txtName.Reset();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";

        this.txtName.Focus(false, 100);
        if (!ExtNet.IsAjaxRequest)
        {
            this.ShowDetail();
        }
    }

    private void ShowDetail()
    {
        Store store = this.txtSubject.GetStore();
        store.DataSource = new SubjectRepository().GetAll();
        store.DataBind();

        if (this.id != "0")
        {
            using (CourseRepository contactRepositiory = new CourseRepository())
            {
                Course entity = contactRepositiory.Get(int.Parse(this.id));
                txtName.Text = entity.Name;
                fckHtmlEditor.Text = entity.Content;
                txtShortContent.Text = entity.ShortContent;
                chkExpired.Checked = entity.HasExpired ?? false;

                if (entity.NumberOfLearner.HasValue)
                    txtNumberOfLearner.Value = entity.NumberOfLearner.Value;

                if (entity.Price.HasValue)
                    txtPrice.Value = entity.Price.Value;

                if (entity.DateStart.HasValue)
                    dtStart.SelectedDate = entity.DateStart.Value;

                if (entity.SubjectID.HasValue)
                {
                    string script = string.Format("setCombo({0});loadEditor();", entity.SubjectID);
                    this.ScriptManager2.Listeners.DocumentReady.Handler = script;
                }
            }
        }
    }

}